package com.edu.hre.struts.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.sql.admin.ProjectDAOService;
import com.edu.hre.struts.form.admin.ProjectForm;
import com.edu.hre.struts.form.admin.ProjectListForm;

public class ProjectListAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(ProjectListAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private static final ProjectDAOService projectDAOService = BeanFinder.getBean(ProjectDAOService.BEAN_NAME, ProjectDAOService.class);
	private final String ACTION_NAME = "projectList";
 
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Print source code location.
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		
		// Get the authorization of the current user for the specified operations. 
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);

		ProjectListForm projectListForm = (ProjectListForm) form;
		
		// Check if the operation is DELETE and the current user is authorized to DELETE.
		if (request.getParameter(StringConstants.BTN_DELETE) != null) {
			projectDAOService.deleteProjects(projectListForm);
		}

		// Set authorization information for the form in order to determine if the ADD or DELETE button is shown.
		projectListForm.setCanAdd(userRoleScreen.isCanCreate());
		projectListForm.setCanDelete(userRoleScreen.isCanDelete());

		// Get the sortBy parameter to determine how the result list is sorted! Then get the list and return.
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			projectListForm.setAsc(!projectListForm.isAsc());
			CommonUtils.sortCollection(projectListForm.getAllProjects(), projectListForm.isAsc(), sortBy);
			return mapping.findForward(ACTION_NAME);
		}
		
		// Paging the displaying list.
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		List<ProjectForm> allProjects = projectDAOService.getProjectListInfo(projectListForm, pageSize, pageSize * (currentPage - 1));
		int countProjects = projectDAOService.countProjects(projectListForm);
		
		projectListForm.setTotalPage(CommonUtils.getTotalPage(countProjects));
		projectListForm.setAllProjects(allProjects);

		ArrayList<HrePageInfo> allPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(countProjects));
		projectListForm.setListPages(allPages);
		projectListForm.setTotalItems(countProjects);

		return mapping.findForward(ACTION_NAME);
	}
}
